package com.feidee.bigdata.bloomfilterserv.init;

import com.alibaba.fastjson.JSON;
import com.feidee.bigdata.bloomfilterserv.constant.Constant;
import com.feidee.bigdata.bloomfilterserv.dao.IBloomFilterDao;
import com.feidee.bigdata.bloomfilterserv.dao.RestoreBfContentThread;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdfalcon.falcon.FalconReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Order(2)
public class ApplicationConfigInit implements ApplicationRunner {

	private Logger logger = LoggerFactory.getLogger(ApplicationRunner.class);
	private static Boolean initFlag = Boolean.TRUE;
	@Autowired
	private Environment env;
	@Autowired
	private IBloomFilterDao bloomFilterDao;

	@Override
	public void run(ApplicationArguments args) throws Exception {

		String[] sourceArgs = args.getSourceArgs();
		logger.info("布隆过滤器服务启动命令行参数:" + JSON.toJSONString(sourceArgs));
		if (null == sourceArgs || sourceArgs.length == 3 || sourceArgs.length == 4) {
			logger.info("!!!!!!!!!!! 启动参数异常 !!!!!!!!!!!!");
			System.exit(-1);
		}
		if (sourceArgs.length >= 5) {
			Constant.RESTORE_BL_SWITCH = sourceArgs[2];
			Constant.BL_HASH_NO = Integer.valueOf(sourceArgs[3]);
			Constant.BL_SERVER_NUM = Integer.valueOf(sourceArgs[4]);
		}

		logger.info(">>>>>>>>>>>>>>>>>>>>>> Environment init start >>>>>>>>>>>>>>>>>>>>");
		logger.info(">>>>>>>>>>>>>>运行环境:" + env.getProperty("spring.profiles.active") + ">>>>>>>>>>>>>>");
		logger.info(">>>>>>>>>>>>>>服务端口:" + env.getProperty("server.port") + ">>>>>>>>>>>>>>>>>>>>>>>>>");
		Constant.ENV = env.getProperty("env", "live");
		CustomConfiguration.setString("env", Constant.ENV);
		Constant.BF_EXCEPTED_INSERTIONS = Integer.valueOf(env.getProperty("bf.excepted.insertions", "25000000"));
		Constant.BF_FPP = Double.valueOf(env.getProperty("bf.fpp", "0.001f"));
		Constant.LOG_PRINT_FREQUENCY = Long.valueOf(env.getProperty("log.print.frequency", "10"));
		bloomFilterDao.clearBF();

		logger.info("Constant.ENV>>>>>>>>>" + Constant.ENV);
		logger.info("Constant.FALCON_START_NAME>>>>>>>>>" + Constant.FALCON_START_NAME);
		logger.info("Constant.BF_EXCEPTED_INSERTIONS>>>>>>>>>" + Constant.BF_EXCEPTED_INSERTIONS);
		logger.info("Constant.BF_FPP>>>>>>>>>" + Constant.BF_FPP);
		logger.info("Constant.UUID_IP_PREFIX>>>>>>>>>" + Constant.UUID_IP_PREFIX);
		logger.info("Constant.LOG_PRINT_FREQUENCY>>>>>>>>>" + Constant.LOG_PRINT_FREQUENCY);
		logger.info("Constant.RESTORE_BL_SWITCH>>>>>>>>>" + Constant.RESTORE_BL_SWITCH);
		logger.info("Constant.BL_HASH_NO>>>>>>>>>" + Constant.BL_HASH_NO);
		logger.info("Constant.BL_SERVER_NUM>>>>>>>>>" + Constant.BL_SERVER_NUM);

		if (initFlag) {
			FalconReportService.start(Constant.FALCON_START_NAME);
			initFlag = Boolean.FALSE;

			if ("1".equals(Constant.RESTORE_BL_SWITCH)) {
				logger.info("启动恢复布隆过滤器服务内容，hash编号:" + Constant.BL_HASH_NO);
				String notificationSql = env.getProperty("restore.notification.sql", "").replace("MSG_CENTER", Constant.MSG_FLAG);
				String msgSql = env.getProperty("restore.msg.sql", "").replace("SMS_CENTER", Constant.SMS_FLAG);
				String emailSql = env.getProperty("restore.email.sql", "").replace("EMAIL_CENTER", Constant.EMAIL_FLAG);
				String url = env.getProperty("db.url", "");
				String username = env.getProperty("db.username", "");
				String password = env.getProperty("db.password", "");
				logger.info("db.url:" + url);
				logger.info("db.username:" + username);
				logger.info("db.password:" + password);
				new Thread(new RestoreBfContentThread(bloomFilterDao, notificationSql, msgSql, emailSql, url,
						username, password)).start();
			} else {
				logger.info("无需恢复布隆过滤器服务内容!");
			}
		}

		logger.info(">>>>>>>>>>>>>>>>>>>>>> Environment init Success >>>>>>>>>>>>>>>>>>>>");

	}

}
